﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;
using Common;

namespace BLL
{
    /// <summary>
    /// 业务层父类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseBLL<T> : IBLL.IBaseBLL<T> where T : class,new()
    {
        public BaseBLL()
        {
            SetDAL();
        }

        //1.数据层接口 对象 - 等待 被实例化
        protected IDAL.IBaseDAL<T> idal;// = new idal.BaseDAL();
        /// <summary>
        /// 由子类实现，为 业务父类 里的 数据接口对象 设置 值！
        /// </summary>
        public abstract void SetDAL();

        //2.增删改查方法
        #region 1.0 新增 实体 +int Add(T model)
        /// <summary>
        /// 新增 实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(T model)
        {
            return idal.Add(model);
        }
        #endregion

        #region 2.0 根据 用户 id 删除 +int Del(int uId)
        /// <summary>
        /// 根据 用户 id 删除
        /// </summary>
        /// <param name="uId"></param>
        /// <returns></returns>
        public int Del(T model)
        {
            return idal.Del(model);
        }
        #endregion

        #region 3.0 根据条件删除 +int DelBy(Expression<Func<T, bool>> delWhere)
        /// <summary>
        /// 3.0 根据条件删除
        /// </summary>
        /// <param name="delWhere"></param>
        /// <returns></returns>
        public int DelBy(Expression<Func<T, bool>> delWhere)
        {
            return idal.DelBy(delWhere);
        }
        #endregion

        #region 4.0 修改 +int Modify(T model, params string[] proNames)
        /// <summary>
        /// 4.0 修改，如：
        /// </summary>
        /// <param name="model">要修改的实体对象</param>
        /// <param name="proNames">要修改的 属性 名称</param>
        /// <returns></returns>
        public int Modify(T model, params string[] proNames)
        {
            return idal.Modify(model, proNames);
        }
        #endregion

        #region  4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        /// <summary>
        ///  4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="whereLambda"></param>
        /// <param name="modifiedProNames"></param>
        /// <returns></returns>
        public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        {
            return idal.ModifyBy(model, whereLambda, modifiedProNames);
        }
        #endregion

        #region 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
        /// <summary>
        /// 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public List<T> GetListBy(Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListBy(whereLambda);
        }
        #endregion

        #region 5.1 根据条件 排序 和查询 + List<T> GetListBy<TKey>
        /// <summary>
        /// 5.1 根据条件 排序 和查询
        /// </summary>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="whereLambda">查询条件 lambda表达式</param>
        /// <param name="orderLambda">排序条件 lambda表达式</param>
        /// <returns></returns>
        public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda)
        {
            return idal.GetListBy(whereLambda, orderLambda);
        }
        #endregion

        #region 5.2 根据条件组、排序字段组，进行查询、排序
        /// <summary>
        /// 5.2 根据条件组、排序字段组，进行查询、排序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="whereLambdas"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public List<T> GetListByConditions<TKey>(List<Expression<Func<T, bool>>> whereLambdas, List<OrderBy<T, TKey>> orderBy)
        {
            return idal.GetListByConditions(whereLambdas, orderBy);
        }
        #endregion

        #region 5.2 根据条件查询对象 + T Find(Expression<Func<T, bool>> whereLambda)
        /// <summary>
        /// 5.2 根据条件查询对象
        /// </summary>
        /// <param name="whereLambda">查询条件 lambda表达式</param>
        /// <returns></returns>
        public T Find(Expression<Func<T, bool>> whereLambda)
        {
            return idal.Find(whereLambda);
        }
        #endregion

        #region 6.0 分页查询 + List<T> GetPagedList<TKey>
        /// <summary>
        /// 6.0 分页查询 + List<T> GetPagedList<TKey>
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="whereLambda">条件 lambda表达式</param>
        /// <param name="orderBy">排序 lambda表达式</param>
        /// <returns></returns>
        public Paged<T> GetPagedList<TKey>(int page, int rows, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy)
        {
            return idal.GetPagedList(page, rows, whereLambda, orderBy);
        }
        #endregion

        #region 6.1 分页查询（条件组） + List<T> GetPagedList<TKey>
        /// <summary>
        /// 6.1 分页查询（条件组） + List<T> GetPagedList<TKey>
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页容量</param>
        /// <param name="whereLambdas">条件组 lambda表达式</param>
        /// <param name="orderBy">排序 lambda表达式</param>
        /// <returns></returns>
        public Paged<T> GetPagedList2<TKey>(int page, int rows, List<Expression<Func<T, bool>>> whereLambdas, Expression<Func<T, TKey>> orderBy)
        {
            return idal.GetPagedList2(page, rows, whereLambdas, orderBy);
        }

        public Paged<T> GetPagedList2<TKey>(int page, int rows, List<Expression<Func<T, bool>>> whereLambdas, Expression<Func<T, TKey>> orderBy, string sortOrder = "asc")
        {
            return idal.GetPagedList2(page, rows, whereLambdas, orderBy, sortOrder);
        }

        #endregion

        #region 6.2 分页查询（查询条件组、排序条件组） + List<T> GetPagedList<TKey>
        public Paged<T> GetPagedList3<TKey>(int page, int rows, List<Expression<Func<T, bool>>> whereLambdas, List<OrderBy<T, TKey>> orderBy)
        {
            return idal.GetPagedList3(page, rows, whereLambdas, orderBy);
        }
        #endregion

        #region 6.3 分页查询 DataTable控件使用 DataTablePaged<T> GetDataTablePaged<TKey>
        /// <summary>
        /// 6.3 分页查询 DataTable控件使用 DataTablePaged<T> GetDataTablePaged<TKey>
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="dataTablesParameters"></param>
        /// <param name="whereLambdas"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public DataTablePaged<T> GetDataTablePagedList(DataTablesParameters dataTablesParameters,
            List<Expression<Func<T, bool>>> whereLambdas, List<OrderBy2> orderBy)
        {
            return idal.GetDataTablePagedList(dataTablesParameters, whereLambdas, orderBy);
        }
        #endregion
    }
}